package com.nio.c1_NettyTest;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @version No one is coming, unless you change yourself!!!
 * @author： pang-yun
 * @date： 2021-05-31 09:19
 */

public class ScatteringReading {
    public static void main(String[] args) {
        //分散写入   多建几个Buffer   channel读取的是一个ByteBuffer的数组

        try (FileChannel fileChannel = new RandomAccessFile("G:\\MyProgran\\play\\Netty-demo\\src\\main\\resources\\ScatteringReading.txt", "r").getChannel()) {
            ByteBuffer allocate1 = ByteBuffer.allocate(5);
            ByteBuffer allocate2 = ByteBuffer.allocate(5);
            ByteBuffer allocate3 = ByteBuffer.allocate(5);
            ByteBuffer allocate4 = ByteBuffer.allocate(5);

            fileChannel.read(new ByteBuffer[]{allocate1,allocate2,allocate3,allocate4});

            read(allocate1);
            System.out.println();
            read(allocate2);
            read(allocate3);
            read(allocate4);


        } catch (IOException e) {
        }
    }


    public static void read(ByteBuffer buffer){
        buffer.flip();
        while (buffer.hasRemaining()){
            byte b = buffer.get();
            System.out.print(String.valueOf((char)b));
        }
    }
}
